﻿using MediatR;
using System.WXT.Domain;
using System.WXT.Domain.DTO;
using System.WXT.Infrstructre.Interface;
using System.WXT.WebAPI.Application.Command.productionPickingCommand;

namespace System.WXT.WebAPI.Application.CommandHandlers.productionPickingCommandHandlers
{
    public class productionPickingShowCommandHandlers:IRequestHandler<productionPickingShowCommand,PageDTO<productionPicking>>
    {
        protected readonly IproductionPickingRepository iproductionPickingRepository;

        public productionPickingShowCommandHandlers(IproductionPickingRepository iproductionPickingRepository)
        {
            this.iproductionPickingRepository = iproductionPickingRepository;
        }
        /// <summary>
        /// 生产领料显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<PageDTO<productionPicking>> Handle(productionPickingShowCommand request, CancellationToken cancellationToken)
        {
            PageDTO<productionPicking> page = new PageDTO<productionPicking>();
            var list = (await iproductionPickingRepository.GetAll()).Where(x => x.productionPickingIsDel == 0);
            if(!string.IsNullOrEmpty(request.productionPickingNumbering))
            {
                list = list.Where(x => x.productionPickingNumbering == request.productionPickingNumbering);
            }
            if(request.productionPickingDate!=null)
            {
                list = list.Where(x => x.productionPickingDate == request.productionPickingDate);
            }
            if(request.productionPickingState!=null)
            {
                list = list.Where(x => x.productionPickingState == request.productionPickingState);
            }
            page.listcount = list.Count();
            page.pagecount = (int)Math.Ceiling(list.Count() * 1.0 / request.pageSize);
            page.plist = list.OrderByDescending(x=>x.productionPickingId).Skip((request.pageIndex-1)*request.pageSize).Take(request.pageSize).ToList();
            return page;

        }
    }
}
